import Crypto from "crypto-js";
import Constants from "common/constants/index";

const cryptKey = Crypto.enc.Utf8.parse(Constants.base.cryptKey);

const CryptoUtils = {
	/**
	 * CryptoJS加密
	 */
	encrypt(data) { //加密
		const encrypted = Crypto.AES.encrypt(
			Crypto.enc.Utf8.parse(data),
			cryptKey,
			{
				mode: Crypto.mode.ECB,
				padding: Crypto.pad.Pkcs7
			});
		return encrypted.toString();
	},

	/**
	 * CryptoJS解密
	 */
	decrypt(data) {//解密
		const decrypt = Crypto.AES.decrypt(
			data,
			cryptKey,
			{
				mode: Crypto.mode.ECB,
				padding: Crypto.pad.Pkcs7
			});
		return Crypto.enc.Utf8.stringify(decrypt).toString();
	}
};

export default CryptoUtils;
